package svc

import (
	"book/service/search/cmd/api/internal/config"
	"book/service/search/cmd/api/internal/middleware"
	"book/service/search/model"
	"book/service/user/cmd/rpc/userclient"

	"github.com/tal-tech/go-zero/core/stores/sqlx"
	"github.com/tal-tech/go-zero/rest"
	"github.com/tal-tech/go-zero/zrpc"
)

type ServiceContext struct {
	Config      config.Config
	SearchModel model.SearchModel
	Example     rest.Middleware
	UserRpc     userclient.User
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.Mysql.DataSource)
	return &ServiceContext{
		Config:      c,
		SearchModel: model.NewSearchModel(conn, c.CacheRedis),
		Example:     middleware.NewExampleMiddleware().Handle,
		UserRpc:     userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
	}
}
